package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.LikesBehaviorService;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApUserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LikesBehaviorServiceImpl implements LikesBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult likes(LikesBehaviorDto dto) {
        //1.todo 检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.todo 用户是否登录 ()
        ApUser user = ApUserThreadLocalUtil.getUser();
        if(user == null || user.getId() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.todo  判断是否是点赞  (REDIS  1.文章   2.用户)   hashSet ==  hashMap  == 关系   hashSet 起始就是使用hashMap的key来进行存储元素的
        //大key   小key   value
        //String    list   zset(不需要分值)     set(文章ID，用户)     Hash(文章ID ， 用户ID ，dto)
        if(dto.getOperation() == 0){
            //4.todo 点赞，   1.给用户添加一个点赞量     2.做作者的文章添加一个点赞量
            Object o = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR+dto.getArticleId().toString(), user.getId().toString());
            if(o != null){
                //代表点过赞了
                return ResponseResult.errorResult(AppHttpCodeEnum.LIKES_EXIST);
            }
            cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR+dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));
            return ResponseResult.okResult("点赞成功");
        }else{
            //5.todo 取消点赞  1.删除用户的点赞信息    2. 删除作者的文章的点赞量
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR+dto.getArticleId().toString(), user.getId().toString());
            return ResponseResult.okResult("取消点赞成功");
        }
    }

    /**
     * 阅读行为记录   （用户可以重复阅读一篇文章）  阅读数需要累加
     * @param dto
     * @return
     */
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        //1.todo 检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.todo 用户是否登录
        ApUser user = ApUserThreadLocalUtil.getUser();
        if(user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.todo 增加阅读行为次数  hash  文章ID  用户ID   count 阅读次数
        String count = (String) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + dto.getArticleId(), user.getId().toString());
        //4.todo 增加阅读量
        if(count == null){
            //第一次阅读
            count = "1";
        }else{
            //累加阅读数量
            count = String.valueOf(Long.parseLong(count) + dto.getCount());
        }
        //5.todo 更新redis 阅读次数
        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR + dto.getArticleId(), user.getId().toString(),count);
        return ResponseResult.okResult(200,"");
    }
}

